﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using DrillOnline.Filters;
using DrillOnline.Models;
using DrillOnline.Services;
using DrillOnline.Services.Scenarios;
using System.Security.Principal;

namespace DrillOnline.Controllers
{
    public class _BaseController : Controller
    {
        public Guid UserTenantId
        {
            get
            {
                return Guid.Parse("5db514e1-69ee-4b26-bffe-354b64e709f9");
            }
        }
        public Tenant UserTenant {
            get {
                return DB.Tenants.Find(UserTenantId);
            }
        }
        protected Models.SiteDbEntities _db;
        public Models.SiteDbEntities DB
        {
            get
            {
                if (null == _db)
                {
                    _db = new SiteDbEntities();
                }
                return _db;
            }
        }

        public IPrincipal SiteUser
        {
            get {
                return User;
            }
        }

        protected DrillServices _svc;
        public DrillServices Services
        {
            get
            {
                if (null == _svc)
                {
                    _svc = new DrillServices() { DB = this.DB, SiteUser = SiteUser, ControllerContext = this };
                }
                return _svc;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (null != _db) _db.Dispose();
            base.Dispose(disposing);
        }

        public DrillScenarios GetCurrentSession()
        {
            if (!ScenariosHost.Instance.Sessions.ContainsKey("default"))
            {
                return null;
            }
            else
            {
                return ScenariosHost.Instance.Sessions["default"] as DrillScenarios;
            }
        }

        public DrillScenarios ThisSession
        {
            get
            {
                if (!ScenariosHost.Instance.Sessions.ContainsKey("default"))
                {
                    return null;
                }
                else
                {
                    return ScenariosHost.Instance.Sessions["default"] as DrillScenarios;
                }
            }
        }

        public Services.Scenarios.Actor ScenariosUser
        {
            get
            {
                var cs = GetCurrentSession();
                var ur = GetUserRole();
                if (ur == "Actor")
                {
                    var a = cs.ActorGroups.Where(v => v.Actors != null).SelectMany(v => v.Actors).FirstOrDefault(v => v.ActorKey == SiteUser.Identity.Name);
                    return a;
                }
                else if (ur == "Examiner")
                {
                    var a = cs.ActorGroups.Where(v => v.Examiners != null).SelectMany(v => v.Examiners).FirstOrDefault(v => v.ActorKey == SiteUser.Identity.Name);
                    return a;
                }
                return null;
            }
        }

        public string GetUserRole(string n)
        {
            //if (!SiteUser.Identity.IsAuthenticated) return "Guest";
            if (n == "管理员") return "Manager";
            var cs = GetCurrentSession();
            if (cs == null ) return "Guest";

            if (cs.ActorGroups.Any(v => v.Examiners.Any(j => j.ActorKey == n))) return "Examiner";

            if (cs.ActorGroups.Any(v => v.Actors.Any(j => j.ActorKey == n))) return "Actor";
            return "Guest";
        }
        public string GetUserRole()
        {
            if (!SiteUser.Identity.IsAuthenticated) return "Guest";
            return GetUserRole(SiteUser.Identity.Name);
        }
        

    }
}
